package com.py.warehouse.controller;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.warehouse.domain.*;
import com.py.warehouse.service.*;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 仓库列表Controller
 * 
 * @author ruoyi
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/warehouse")
public class BsWarehouseController extends BaseController
{
    @Autowired
    private IBsWarehouseService bsWarehouseService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysUserService iSysUserService;

    @Resource
    private PyCodeApi pyCodeApi;

    @Resource
    private IPyAttachmentService pyAttachmentService;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code") Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }


    /**
     * 库存数量占比
     */
    @GetMapping("/quantity")
    public WarehouseWithTotal selectBsWarehouseQuantity(){
        System.out.println("库存数量:"+bsWarehouseService.selectBsWarehouseQuantity());
        return bsWarehouseService.selectBsWarehouseQuantity();
    }

    /**
     * 库存金额占比
     */
    @GetMapping("/money")
    public WarehouseWithTotalMoney selectBsWarehouseMoney(){
        System.out.println("库存数量:"+bsWarehouseService.selectBsWarehouseMoney());
        return bsWarehouseService.selectBsWarehouseMoney();
    }


    /**
     * 查询仓库列表列表
     */
    @RequiresPermissions("warehouse:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo list(BsWarehouse bsWarehouse)
    {
        startPage();
        List<BsWarehouse> list = bsWarehouseService.selectBsWarehouseList(bsWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库列表列表
     */
    @RequiresPermissions("warehouse:warehouse:export")
    @Log(title = "仓库列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsWarehouse bsWarehouse)
    {
        List<BsWarehouse> list = bsWarehouseService.selectBsWarehouseList(bsWarehouse);
        ExcelUtil<BsWarehouse> util = new ExcelUtil<BsWarehouse>(BsWarehouse.class);
        util.exportExcel(response, list, "仓库列表数据");
    }

    /**
     * 获取仓库列表详细信息
     */
    @RequiresPermissions("warehouse:warehouse:query")
    @GetMapping(value = "/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(bsWarehouseService.selectBsWarehouseByWarehouseId(warehouseId));
    }

    /**
     * 新增仓库列表
     */
    @RequiresPermissions("warehouse:warehouse:add")
    @Log(title = "仓库列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsWarehouse bsWarehouse)
    {
        System.out.println("新增仓库");
        pyCodeApi.saveCode(bsWarehouse.getCodeId(),bsWarehouse.getNumber());

        //添加盘点单
        List<Object> fileList = bsWarehouse.getFileList();
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("仓库单");
                            pyAttachment.setOwnershipNumber(bsWarehouse.getWarehouseCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }

        return toAjax(bsWarehouseService.insertBsWarehouse(bsWarehouse));
    }

    /**
     * 修改仓库列表
     */
    @RequiresPermissions("warehouse:warehouse:edit")
    @Log(title = "仓库列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsWarehouse bsWarehouse)
    {
        //删除盘点单
        pyAttachmentService.deletePyAttachmentByOwnershipNumber(bsWarehouse.getWarehouseCode());

        //添加盘点单
        List<Object> fileList = bsWarehouse.getFileList();
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("仓库单");
                            pyAttachment.setOwnershipNumber(bsWarehouse.getWarehouseCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }

        return toAjax(bsWarehouseService.updateBsWarehouse(bsWarehouse));
    }

    /**
     * 删除仓库列表
     */
    @RequiresPermissions("warehouse:warehouse:remove")
    @Log(title = "仓库列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        System.out.println("删除");
        return toAjax(bsWarehouseService.deleteBsWarehouseByWarehouseIds(warehouseIds));
    }

    /**
     * 查询部门
     */
    @RequiresPermissions("warehouse:warehouse:dept")
    @GetMapping("/dept")
    public List<SysDept> selectDept()
    {
        List<SysDept> list = iSysDeptService.selectDept();
        return list;
    }

    /**
     * 查询仓库主管
     */
    @RequiresPermissions("warehouse:warehouse:user")
    @GetMapping("/user")
    public List<SysUser> selectUser()
    {
        List<SysUser> list = iSysUserService.selectUser();
        return list;
    }

    /**
     * 仓库文件
     */
    @GetMapping("/getAttachment")
    public List<PyAttachment> getAttachment(@RequestParam("code")String code)
    {
        List<PyAttachment> list = pyAttachmentService.selectPyAttachmentByOwnershipNumber(code);
        return list;
    }

    @GetMapping("selectAll")
    public List<BsWarehouse> selectAll(){
        List<BsWarehouse> list = bsWarehouseService.selectAll();

        return list;
    }
}
